<?php

Class Pet extends Connect{
    
    static $pets = array();
    
    static function getPets(){
        
        if(count(Pet::$pets) > 0) return Pet::$pets;
        
        #$sql = "SELECT * FROM `".PREFIX_STATIC."pet`";
        $sql = "SELECT  pet.id as id,
				        pet.name as name,
                        pet.descript as descript,
                        pet.buy1 as buy1,
                        pet.buy2 as buy2,
                        pet.sell1 as sell1,
                        pet.sell2 as sell2,
                        pet.level as level,
                        pet.image as image,
                        pet.thumbimage as thumbimage,
                        
                        skin.id as skinid,
                        skin.descript as skindescript,
                        skin.parent as skinparent,                                                
                        skin.level as skinlevel,
                        skin.thumbimage as skinthumbimage,                         
                        skin.image as skinimage,
                        skin.type as skintype
                          
                FROM (SELECT * FROM  `".PREFIX_STATIC."pet` WHERE `parent` = '0') pet
                LEFT JOIN
                (SELECT * FROM  `".PREFIX_STATIC."pet`) skin ON pet.id = skin.parent ";
        #return $sql;
        if(!$result = DBConnect::query($sql, 'pet')){
            return mysql_error();
        }               
        while($arr = DBConnect::getOne($result, 'pet')){
            $i = $arr['id'];
            if(!isset(Pet::$pets[$i])){
                Pet::$pets[$i] = array();// $arr;
                Pet::$pets[$i]['id'] = $arr['id'];
                Pet::$pets[$i]['name'] = $arr['name'];
                Pet::$pets[$i]['descript'] = $arr['descript'];
                                
                Pet::$pets[$i]['buy1'] = $arr['buy1'];
                Pet::$pets[$i]['buy2'] = $arr['buy2'];
                Pet::$pets[$i]['sell1'] = $arr['sell1'];
                Pet::$pets[$i]['sell2'] = $arr['sell2'];
                Pet::$pets[$i]['level'] = $arr['level'];
                Pet::$pets[$i]['image'] =  ( !Pet::is_url($arr['image']) ? MEDIA_URL.$arr['image'] : $arr['image']  ) ;
                Pet::$pets[$i]['thumbimage'] =  ( !Pet::is_url($arr['thumbimage']) ? MEDIA_URL.$arr['thumbimage'] : $arr['thumbimage']  ) ;
                
                Pet::$pets[$i]['skins'] = array();// $arr['image'];    
            }
            
            
            Pet::$pets[$i]['skins'][$arr['skinlevel']]['id'] = $arr['skinid'];
            Pet::$pets[$i]['skins'][$arr['skinlevel']]['level'] = $arr['skinlevel']; 
            Pet::$pets[$i]['skins'][$arr['skinlevel']]['type'] = $arr['skintype'];
            Pet::$pets[$i]['skins'][$arr['skinlevel']]['descript'] = $arr['skindescript'];
            if($arr['skintype'] == 0){
                Pet::$pets[$i]['skins'][$arr['skinlevel']]['image'] = ( !Pet::is_url($arr['skinimage']) ? MEDIA_URL.$arr['skinimage'] : $arr['skinimage']  ) ;    
            }else{
                Pet::$pets[$i]['skins'][$arr['skinlevel']]['anime'] = ( !Pet::is_url($arr['skinimage']) ? MEDIA_URL.$arr['skinimage'] : $arr['skinimage']  ) ;
            }
            Pet::$pets[$i]['skins'][$arr['skinlevel']]['thumbimage'] = ( !Pet::is_url($arr['skinthumbimage']) ? MEDIA_URL.$arr['skinthumbimage'] : $arr['skinthumbimage']  ) ;
        }                   
        return Pet::$pets;
    }
    
    static function getIPets(){
        Pet::getPets();
        foreach(Pet::getPets() as $val){
            $_arr[] = $val;
        }      
        return $_arr;
    }
    
    
    function savePet($data){
        $sql = "UPDATE `".PREFIX_STATIC."pet` SET `name` = '".$data['name']."', 
                                                  `descript` = '".$data['descript']."',
                                                  `image` = '".$data['image']."' 
                                                  WHERE `parent` = '0' ";
        if(!$result = DBConnect::query($sql, 'pet')){
            $this->error(mysql_error()) ;
            return false;
        }
        foreach( $data['skins'] as $k => $v ){
            $sql = "UPDATE `".PREFIX_STATIC."pet` SET `image` = '".$v['image']."', `thumbimage` = '".$v['thumbimage']."' WHERE `parent` = '1' AND `level` = '".$v['level']."' AND `type` = '0' ";
            DBConnect::query($sql);
            $sql = "UPDATE `".PREFIX_STATIC."pet` SET `image` = '".$v['anime']."', `thumbimage` = '".$v['thumbimage']."' WHERE `parent` = '1' AND `level` = '".$v['level']."' AND `type` = '1' ";
            DBConnect::query($sql);                        
        }
        return true; 
    }
    
    
    static function is_url($str){
        if(substr($str, 0, 4) == "http"){
            return true;
        }
        if(substr($str, 0, 3) == "ftp"){
            return true;
        }
        return false;
    }
}
?>